package com.lyf.language.practice.practice_6_21.io.commonio;

import io.netty.util.concurrent.DefaultThreadFactory;
import org.junit.Test;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * IO流
 * 1.基本分类
 * （1）按方向（以程序本身为基准）：输入输出
 * （2）处理方式：处理流、节点流（FileStream..)
 * （3）字节流、字符流
 *  *  ----------------————————————————————————————---------——————
 *  * .    字节流（视频、图片等）       字符流（char字符，文本）
 *  * .    I/O Stream               Reader/Writer
 *  * -------------------------------------------------------------
 *  * 节点流（File I/O Stream）\处理流（外层包裹）
 * @author LYF
 * @date 2022/6/22
 */
public class IoTests {

    final String scrDir = "E:\\JavaProjects\\LearnProjects\\learn-practice\\java-base\\src\\main\\java\\com\\lyf\\language\\practice\\practice_6_21\\io\\commonio\\files\\src";
    final String targetDir = "E:\\JavaProjects\\LearnProjects\\learn-practice\\java-base\\src\\main\\java\\com\\lyf\\language\\practice\\practice_6_21\\io\\commonio\\files\\target";

    @Test
    public void testWriteToFile(){
        FileOutputStream fos = null;
        try {
            fos  = new FileOutputStream("");
        }catch (FileNotFoundException e){
        }finally {
        }
    }


    /*
    多线程实现文件的搬运
     */
    @Test
    public void test(){
        recursionSearch(scrDir,targetDir);
        //transfer(scrDir+"\\hello.txt",targetDir+"\\hello.txt");
    }

    ThreadPoolExecutor executor =
            new ThreadPoolExecutor(5,9,
                    2, TimeUnit.SECONDS,new LinkedBlockingQueue<>());

    void recursionSearch(String scrDir1,String targetDir1){
        File file = new File(scrDir1);
        for(File file1: Objects.requireNonNull(file.listFiles())){
            if(file1.isFile()){
//                System.out.println(file1.getPath());
//                System.out.println(file1.getPath().substring(scrDir.length()));
                String suffix = file1.getPath().substring(scrDir.length());
//                transfer(file1.getPath(),targetDir+""+suffix);
                executor.submit(()->{
                    transfer2(file1.getPath(),targetDir+""+suffix);
                });

            }else {
                //System.out.println(file1.getPath());
                recursionSearch(file1.getPath(),"");
            }
        }
    }

    /*
    Files工具类
     */
    void transfer(String fileSrc,String fileTarget){
        try {
            Files.copy(Path.of(fileSrc),Path.of(fileTarget));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*
    IO 流方式
     */

    void transfer2(String fileSrc,String fileTarget){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try{
            fis = new FileInputStream(fileSrc);
            fos = new FileOutputStream(fileTarget);
            byte[] bytes = new byte[1024];
            int readN;
            while((readN=fis.read(bytes))!=-1){

                fos.write(bytes,0,readN);// 图片和pdf出问题--
               // fos.write(bytes);

            }

            fos.flush();//注意刷新，否则图片不出来
            //ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(fis);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(fis!=null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fos!=null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }






}
